{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ca355dd3",
   "metadata": {},
   "source": [
    "## 注释"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5aa160dc",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-13T04:17:25.026378Z",
     "start_time": "2023-09-13T04:17:25.021736Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n    第一行注释\\n    第二行注释\\n    第三行注释\\n'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 单行注释\n",
    "\n",
    "'''\n",
    "    第一行注释\n",
    "    第二行注释\n",
    "    第三行注释\n",
    "'''\n",
    "\n",
    "\"\"\"\n",
    "    第一行注释\n",
    "    第二行注释\n",
    "    第三行注释\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ede5dbd",
   "metadata": {},
   "source": [
    "## 变量"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70a56430",
   "metadata": {},
   "source": [
    "### 定义变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f87b3246",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-13T04:17:25.029749Z",
     "start_time": "2023-09-13T04:17:25.027244Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n标识符命名规则：\\n    由数字、字母、下划线组成\\n    不能数字开头\\n    不能使用内置关键字\\n    严格区分大小写\\n    \\n命名习惯：\\n    见名知义\\n    大驼峰：每个单词首字母大写 MyName\\n    小驼峰：第二个单词及以后的单词首字母大写 myName\\n    下划线：my_name\\n    \\n变量的命名 不要和函数名一样————不然会导致函数无法调用\\n'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "## 1、定义变量————python定义变量 不需要声明类型\n",
    "num1 = 2\n",
    "str1 = \"python\"\n",
    "\n",
    "# python可以多重赋值\n",
    "num2, num3, str2 = 10 , 10.3 , \"number\"\n",
    "\n",
    "'''\n",
    "标识符命名规则：\n",
    "    由数字、字母、下划线组成\n",
    "    不能数字开头\n",
    "    不能使用内置关键字\n",
    "    严格区分大小写\n",
    "    \n",
    "命名习惯：\n",
    "    见名知义\n",
    "    大驼峰：每个单词首字母大写 MyName\n",
    "    小驼峰：第二个单词及以后的单词首字母大写 myName\n",
    "    下划线：my_name\n",
    "    \n",
    "变量的命名 不要和函数名一样————不然会导致函数无法调用\n",
    "'''"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40a8ebcc",
   "metadata": {},
   "source": [
    "### 使用变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "444edfb3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-13T04:17:25.033612Z",
     "start_time": "2023-09-13T04:17:25.030980Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "马丽亚\n",
      "标识 4350940912\n",
      "类型 <class 'str'>\n",
      "值 马丽亚\n"
     ]
    }
   ],
   "source": [
    "## 2、使用变量\n",
    "name='马丽亚'\n",
    "print(name)\n",
    "print('标识',id(name)) # 内存地址\n",
    "print('类型',type(name))\n",
    "print('值',name)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15ee0340",
   "metadata": {},
   "source": [
    "### Python中的保留字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2fd11aec",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-13T04:17:25.036486Z",
     "start_time": "2023-09-13T04:17:25.034483Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['False', 'None', 'True', '__peg_parser__', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']\n"
     ]
    }
   ],
   "source": [
    "## 3、Python中的保留字\n",
    "import keyword\n",
    "print(keyword.kwlist)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e2ed99d",
   "metadata": {},
   "source": [
    "## 认识数据类型"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3edc6ca2",
   "metadata": {},
   "source": [
    "### int"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c0c138d6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-13T04:17:25.038774Z",
     "start_time": "2023-09-13T04:17:25.037374Z"
    }
   },
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "1、按经验将不同的变量存储不同的类型的数据\n",
    "\n",
    "2、验证这些数据到底是什么类型 -- 检测数据类型 -- type(数据)\n",
    "\"\"\"\n",
    "\n",
    "# int -- 整型\n",
    "num1 = 1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39306034",
   "metadata": {},
   "source": [
    "### float"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "55c00ab8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-13T04:17:25.041679Z",
     "start_time": "2023-09-13T04:17:25.039662Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'int'>\n",
      "<class 'float'>\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# float -- 浮点型，就是小数\n",
    "num2 = 1.1\n",
    "print(type(num1))\n",
    "print(type(num2))\n",
    "print(0.1 + 0.2 == 0.3)  # False 由于精度问题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff52bc69",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-13T04:17:25.045985Z",
     "start_time": "2023-09-13T04:17:25.042599Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4.376160544\n"
     ]
    }
   ],
   "source": [
    "# decimal库\n",
    "# float 最多精确到 15位————要想让小数更加精确————利用decimal库\n",
    "import decimal\n",
    "decimal1 = decimal.Decimal('3.141592653')\n",
    "decimal2 = decimal.Decimal('1.234567891')  # 里面的字符串 只能用单引号‘’\n",
    "decimal.getcontext().prec = 20  # 设置数据类型的 精确度\n",
    "print(decimal1 + decimal2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "142d4bbf",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-13T04:17:25.048355Z",
     "start_time": "2023-09-13T04:17:25.046723Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0043\n"
     ]
    }
   ],
   "source": [
    "# e/E 的使用\n",
    "# <a>e<b> == a * 10**b\n",
    "print(4.3e-3)  # 0.0043"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55cd34ab",
   "metadata": {},
   "source": [
    "### 复数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cd02400b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-13T04:17:25.052299Z",
     "start_time": "2023-09-13T04:17:25.050558Z"
    }
   },
   "outputs": [],
   "source": [
    "# 复数类型————虚数部分用j\n",
    "real1 = (1.23e-4+5.67e+89j).real #取复数的 实数部分\n",
    "imag1 = (1.23e-4+5.67e+89j).imag #取复数的 虚数部分"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d6f6253",
   "metadata": {},
   "source": [
    "### str"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "dfaa9d89",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-13T04:17:25.055208Z",
     "start_time": "2023-09-13T04:17:25.053047Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "人生苦短，我用Python <class 'str'>\n",
      "人生苦短，我用Python <class 'str'>\n",
      "人生苦短，\n",
      "我用Python <class 'str'>\n",
      "人生苦短，\n",
      "我用Python <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "# str -- 字符串，特点：数据都要带引号\n",
    "str1='人生苦短，我用Python'\n",
    "str2=\"人生苦短，我用Python\"\n",
    "str3=\"\"\"人生苦短，\n",
    "我用Python\"\"\"\n",
    "\n",
    "str4='''人生苦短，\n",
    "我用Python'''\n",
    "print(str1,type(str1))\n",
    "print(str2,type(str2))\n",
    "print(str3,type(str3)) # 包括了换行\n",
    "print(str4,type(str4)) # 包括了换行"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4680e3ec",
   "metadata": {},
   "source": [
    "### bool"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bdd9cb29",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-13T04:17:25.057891Z",
     "start_time": "2023-09-13T04:17:25.056025Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'bool'>\n",
      "2\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "# bool -- 布尔型，通常判断使用，布尔型有两个取值 True 和 False\n",
    "b = True\n",
    "print(type(b))\n",
    "\n",
    "# 布尔值可以转成整数计算\n",
    "print(True + 1)  #2     1+1的结果为2  True表示1\n",
    "print(False + 1)  #1    0+1的结果为1  False表示0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fab2ce48",
   "metadata": {},
   "source": [
    "### list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "77259c2f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-13T04:17:25.060344Z",
     "start_time": "2023-09-13T04:17:25.058602Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# list -- 列表\n",
    "c = [10, 20, 30]\n",
    "print(type(c))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "71019848",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-13T04:16:36.294119Z",
     "start_time": "2023-09-13T04:16:36.291585Z"
    }
   },
   "source": [
    "### tuple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4bf9ee05",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-13T04:17:25.063170Z",
     "start_time": "2023-09-13T04:17:25.061407Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "# tuple -- 元组\n",
    "d = (10, 20, 30)\n",
    "print(type(d))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3062aed9",
   "metadata": {},
   "source": [
    "### set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "98363bf3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-13T04:17:25.066376Z",
     "start_time": "2023-09-13T04:17:25.064227Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'set'>\n"
     ]
    }
   ],
   "source": [
    "# set -- 集合\n",
    "e = {10, 20, 30}\n",
    "print(type(e))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe19f510",
   "metadata": {},
   "source": [
    "### dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f840eb7b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-13T04:17:25.069071Z",
     "start_time": "2023-09-13T04:17:25.067220Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "# dict -- 字典 -- 键值对\n",
    "f = {'name': 'TOM', 'age': 18}\n",
    "print(type(f))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16e3fdb2",
   "metadata": {},
   "source": [
    "## 输出"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64ac778a",
   "metadata": {},
   "source": [
    "### print()语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d228a04b",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 1、认识print()语句 ##\n",
    "# print函数的格式： print(value , …… , sep=' ' , end='\\n' , file=sys.stdout , flush= False)\n",
    "# value：要输出的值，可以是多个\n",
    "# sep：多个值之间的分隔符， 默认为‘ ’\n",
    "# end：结束符， 默认为‘\\n’\n",
    "# file: 输出流，默认为sys.stdout\n",
    "# flush： 是否刷新到输出流——默认 否\n",
    "\n",
    "print(\"python\")  # 换行打印\n",
    "print('hello','world','Python')\n",
    "\n",
    "print(\"python\", end='\\t')  # 不换行打印\n",
    "print(\" 同一行的python\")\n",
    "\n",
    "name = '小明'\n",
    "score = 89\n",
    "print(3 * (\"我的名字叫\" + name + \",我考了\" + str(score) + \"分\"))  # 字符串拼接\n",
    "# 注意拼接时，都要变成字符串\n",
    "print('=================================================')\n",
    "'''\n",
    "print可将数据输出文件中\n",
    "注意点:\n",
    "    1.所指定的盘符在存在，\n",
    "    2.使用 file= fp\n",
    "'''\n",
    "fp = open('text.txt', 'a+') # a+如果文件不存在就创建，存在就在文件内容的后面继续追加\n",
    "print('helloworld', file=fp)\n",
    "fp.close()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a8bfd758",
   "metadata": {},
   "source": [
    "### 格式化字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7c9ed9f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 1、格式化输出的一般方法 ##\n",
    "'''\n",
    "    %d 填充整数\n",
    "    %06d 表示输出的整数显示6位数，不足以0补全，超出则原样输出\n",
    "\n",
    "    %s 填充字符串，如果填充不是字符串，则转为字符串\n",
    "    %f 填充浮点数，默认保留6位小数   %.2f保留2位小数  %10.2f保留2位小数，总宽度为10\n",
    "\n",
    "    也可以使用 f'我的名字是{表达式A}，小名是{表达式B}'\n",
    "'''\n",
    "age = 18\n",
    "name = 'TOM'\n",
    "weight = 75.5\n",
    "stu_id = 1\n",
    "stu_id2 = 9999\n",
    "\n",
    "# (1)今年我的年龄是x岁 -- 整数 %d   %06d 表示输出的整数显示6位数，不足以0补全，超出则原样输出\n",
    "print('今年我的年龄是%06d岁' % age)\n",
    "print('今年我的年龄是%s岁' % age)\n",
    "print(f'今年我的年龄是{age}岁')\n",
    "print('=================================================')\n",
    "\n",
    "# (2)我的名字是x -- 字符串 %s\n",
    "print('我的名字是%s' % name)\n",
    "print('=================================================')\n",
    "\n",
    "# (3)我的体重是x公斤 -- 浮点数 %6.2f 保留2位小数，总长度为6\n",
    "print('我的体重是%6.2f公斤' % weight)\n",
    "print('=================================================')\n",
    "\n",
    "# (4)我的学号是x\n",
    "print('我的学号是%d' % stu_id)\n",
    "print('我的学号是%03d' % stu_id)\n",
    "print('我的学号是%03d' % stu_id2)\n",
    "print('=================================================')\n",
    "\n",
    "# (5)我的名字是x，今年x岁了\n",
    "print('我的名字是%s，今年%d岁了' % (name, age))\n",
    "print('我的名字是%s，明年%d岁了' % (name, age + 1))\n",
    "print('=================================================')\n",
    "\n",
    "# (6)我的名字是x，今年x岁了，体重x公斤，学号是x\n",
    "print('我的名字是%s，今年%d岁了，体重%.2f公斤，学号是%03d' % (name, age, weight, stu_id))\n",
    "print('我的名字是%s，今年%s岁了，体重%s公斤，学号是%s' % (name, age, weight, stu_id))\n",
    "print(f'我的名字是{name},今年{age}岁了，体重{weight}公斤，学号是{stu_id}')\n",
    "print('=================================================')\n",
    "\n",
    "\n",
    "## 2、利用format()方法，对字符串类型的格式化处理 ##\n",
    "# 利用format()方法————格式化：随时改变 字符串中 某部分的内容————将要修改的内容，用{}隔开\n",
    "\"{}:计算机{}的CPU占用率为{}%。\".format('2016-12-31', '外星人', 10)  # format()里的参数，按顺序填入{}内\n",
    "\"{1}:计算机{0}的CPU占用率为{2}%。\".format('2016-12-31', '外星人', 10)  # format()里的参数，按标记好的顺序 填入{}内\n",
    "\n",
    "# 若调用format()方法时，希望{}本身是 字符串的一部分 ，则需要如下形式：\n",
    "print('圆周率{{{1}{2}}}是{0}'.format('无理数', 3.1415926, '……'))\n",
    "\n",
    "\"\"\"\n",
    "{}内部，可以填入参数序号，还可以填入 格式控制信息————{<参数序号> : <格式控制标记>}\n",
    "以下来介绍 格式控制标记：\n",
    "    <填充>        <对齐>      \t\t    <宽度>                  <,>   \t\t    <.精度>          \t\t\t             <类型>\n",
    "    默认空格\t   <左对齐\t\t        先设置为3\t\t\t\t 千位分隔符\t 10.2保留2为小数，长度为10\t\t    整数类型\t\t\t\t\t浮点数类型\n",
    "    设置*\t\t    >右对齐\t        参数为4位，则输出4位\t\t\t\t\t\t.2 字符串输出2位\t\t\t     b 二进制\t\t\t\t    e 对应小写字母e的指数形式\n",
    "\t\t\t     ^中对齐\t       参数为2位，则填充字符占1位\t\t\t\t\t\t\t\t\t\t\t    c 对应的字符          \t  E 对应大写字母E的指数形式\n",
    "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t                  d 十进制\t\t\t\t     f 标准浮点形式\n",
    "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t                  o 八进制\t\t\t\t     % 浮点数的百分形式\n",
    "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t                  x 小写十六进制\n",
    "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t                  X 大写十六进制\n",
    "\"\"\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91206b83",
   "metadata": {},
   "source": [
    "### 转义字符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "01c7665c",
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "    \\n: 换行\n",
    "    \\t: 制表符 -- 利用空格，将一个制表位（4个单元格）占满\n",
    "    \\r: 光标回到行首，如果继续输入，则覆盖该行原本内容\n",
    "    \\b: 退一个 -- 删除前一个字符\n",
    "    \\: 消除特殊字符的作用，变成普通字符输出\n",
    "    R/r: 原字符，不希望字符串中的转义字符起作用，就使用原字符，就是在字符串之前加上r,或R\n",
    "\"\"\"\n",
    "\n",
    "print('hello')\n",
    "print('world')\n",
    "print('===========')\n",
    "# \\n: 换行\n",
    "print('hello\\nworld')\n",
    "\n",
    "print('===========')\n",
    "# \\t: 制表符 -- 利用空格，将一个制表位（4个单元格）占满\n",
    "print('hello\\tworld')\n",
    "print('helloooo\\tworld')\n",
    "\n",
    "print('===========')\n",
    "# \\r: 光标回到行首，如果继续输入，则覆盖该行原本内容\n",
    "print('hello\\rworld')\n",
    "\n",
    "print('===========')\n",
    "# \\b：退一个格，将o退没了\n",
    "print('hello\\bworld')\n",
    "\n",
    "print('===========')\n",
    "# \\: 消除特殊字符的作用，变成普通字符输出\n",
    "print('http:\\\\\\\\www.baidu.com')\n",
    "print('老师说:\\'大家好\\'')\n",
    "\n",
    "print(\"===========\")\n",
    "# R/r: 原字符，不希望字符串中的转义字符起作用，就使用原字符，就是在字符串之前加上r,或R\n",
    "print(r'hello\\nworld')\n",
    "\n",
    "# 注意事项， 最后一个字符不能是反斜杠\n",
    "# error: print(r'hello\\nworld\\')\n",
    "print(r'hello\\nworld\\\\')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea833a57",
   "metadata": {},
   "source": [
    "## 输入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9e20668f",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 使用input()函数 ##\n",
    "\"\"\"\n",
    "格式:\n",
    "\tinput('提示符')————返回 用户输入的 字符串————若要输入其他数据类型  则要转换\n",
    "\n",
    "特点：\n",
    "    1、当程序执行到input，等待用户输入，输入完成之后才继续向下执行。\n",
    "    2、input接收用户输入后，一般存储到变量，方便使用\n",
    "    3、input会把接收到的任意用户输入的数据都当做 字符串 处理\n",
    "\"\"\"\n",
    "\n",
    "age = input('请输入您的年龄:\\n')\n",
    "print(age)\n",
    "print(type(age))\n",
    "print('==================')\n",
    "# eval()  // 将 括号内的 字符串 以 表达式的形式 计算出结果 并 返回\n",
    "print(eval(\" 16*54 - 64 +128//2 \"))\n",
    "\n",
    "score = eval(input(\"请输入你的分数:\\n\"))\n",
    "print(type(score))\n",
    "print('您的分数是%d' % (score + 10))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "782fb987",
   "metadata": {},
   "source": [
    "## 数据类型的转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "00478afb",
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "体验数据类型转换：\n",
    "    1、input\n",
    "\n",
    "    2、检测input数据类型str\n",
    "\n",
    "    3、int() 转换数据类型\n",
    "\n",
    "    4、检测是否转换成功\n",
    "\n",
    "\"\"\"\n",
    "num = input('请输入数字：\\n')\n",
    "print(num)\n",
    "print(type(num))\n",
    "\n",
    "num = int(num)\n",
    "print(type(num))\n",
    "print('================')\n",
    "\n",
    "## 数据类型转换函数 ##\n",
    "# 1、float() -- 将数据转换成浮点型\n",
    "# 显式转换 -- 使用float函数\n",
    "s1 = '128.98'\n",
    "s2 = '76'\n",
    "ff = True\n",
    "s3 = 'hello'\n",
    "i = 98\n",
    "print(type(s1), type(s2), type(ff), type(s3), type(i))\n",
    "print(float(s1), type(float(s1)))\n",
    "print(float(s2), type(float(s2)))\n",
    "print(float(ff), type(float(ff)))\n",
    "# error: print(float(s3),type(float(s3))) # 字符串中的数据如果是非数字串，则不允许转换\n",
    "print(float(i), type(float(i)))\n",
    "\n",
    "# 隐式转换\n",
    "print(type(10 + 0.1))\n",
    "print('================')\n",
    "\n",
    "# 2、str() -- 将数据转换成字符串\n",
    "name = '张三'\n",
    "age = 20\n",
    "print(type(name), type(age))  # 说明name与age的数据类型不相同\n",
    "# error: print('我叫'+name+'今年,'+age+'岁')  #当将str类型与int类型进行连接时，报错，解决方案，类型转换\n",
    "print('我叫' + name + '今年,' + str(age) + '岁')\n",
    "\n",
    "print(\"================\")\n",
    "\n",
    "# 3、int() -- 将数据转换成整型\n",
    "s1 = '128'\n",
    "f1 = 98.7\n",
    "s2 = '76.77'\n",
    "ff = True\n",
    "s3 = 'hello'\n",
    "print(type(s1), type(f1), type(s2), type(ff), type(s3))\n",
    "\n",
    "print(int(s1), type(int(s1)))  # 将str转成int类型 ,字符串为 数字串\n",
    "print(int(f1), type(int(f1)))  # float转成int类型，截取整数部分，舍掉小数部分\n",
    "# error: print(int(s2),type(int(s2)))  #将str转成int类型，报错，因为字符串为小数串\n",
    "print(int(ff), type(int(ff)))\n",
    "# error: print(int(s3),type(int(s3)))  #将str转成int类型时，字符串必须为数字串（整数），非数字串是不允许转换\n",
    "print(\"================\")\n",
    "\n",
    "# 4、tuple() -- 将一个序列转换成元组\n",
    "list1 = [10, 20, 30]\n",
    "print(tuple(list1))\n",
    "print(\"================\")\n",
    "\n",
    "# 5、list() -- 将一个序列转换成列表\n",
    "t1 = (10, 20, 30)\n",
    "print(list(t1))\n",
    "print(\"================\")\n",
    "\n",
    "# 6、eval() -- 将 括号内的 字符串 以 表达式的形式 计算出结果 并 返回\n",
    "str2 = '1'\n",
    "str3 = '1.1'\n",
    "str4 = '(1000, 2000, 3000)'\n",
    "str5 = '[1000, 2000, 3000]'\n",
    "print(type(eval(str2)))\n",
    "print(type(eval(str3)))\n",
    "print(type(eval(str4)))\n",
    "print(type(eval(str5)))\n",
    "print(\"================\")\n",
    "\n",
    "## 字符 ————> Unicode\n",
    "ord('c')\n",
    "\n",
    "## Unicode ————> 字符\n",
    "chr(3)\n",
    "\n",
    "## round() -- 四舍六入规则——如果是5，则取最接近的偶数\n",
    "round(7.7)  # 结果： 8\n",
    "round(7.4)  # 结果： 7\n",
    "round(-9.5)  # 结果： -10 取最接近的偶数\n",
    "round(3.1415926, 3)  # 对3.1415926 四舍六入————并保留3位小数\n",
    "\n",
    "## 其他类型————>复数\n",
    "\"\"\"\n",
    "complex(real,imag) # real为实部————可以是 整数、浮点数、字符串\n",
    "\t\t\t\t   # imag为虚部————可以省略————可以是 整数、浮点数， 不能是字符串\n",
    "\"\"\"\n",
    "complex(3, 4)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c69288fc",
   "metadata": {},
   "source": [
    "## 进制之间的转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "88b685d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "### 进制之间的转换 ###\n",
    "\n",
    "# 十进制 ——> 二进制\n",
    "a = 10\n",
    "print(bin(a))\n",
    "\n",
    "# 二进制 ——> 十进制\n",
    "b = '10111001'\n",
    "print(int(b,2))\n",
    "\n",
    "b = 0b10111001\n",
    "print(b)\n",
    "\n",
    "# 十进制 ——> 八进制\n",
    "a = 32\n",
    "print(oct(a))\n",
    "\n",
    "# 八进制 ——> 十进制\n",
    "b = '756'\n",
    "print(int(b,8))\n",
    "\n",
    "b = '0o756'\n",
    "print(eval(b))\n",
    "\n",
    "# 十进制 ——> 十六进制\n",
    "a = 87\n",
    "print(hex(a))\n",
    "\n",
    "# 十六进制 ——> 十进制\n",
    "b = 'A34'\n",
    "print(int(b,16))\n",
    "\n",
    "b = '0xA34'\n",
    "print(eval(b))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ded089da",
   "metadata": {},
   "source": [
    "## 二进制与字符串编码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9635ad3e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# chr: 数字 -> 字符\n",
    "print(chr(0b100111001011000))\n",
    "\n",
    "# ord: 字符 -> 数字\n",
    "print(ord('乘'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0285d179",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "288px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
